package com.demo.common.base.pojo;

import com.demo.common.base.constant.LogConstant;
import lombok.Data;
import org.slf4j.MDC;

@Data
public class JsonRes<T> {
    // 响应业务状态
    private Integer status;

    // 响应消息
    private String msg;

    // 响应中的数据
    private T data;

    private String traceId;

    public static <T> JsonRes<T> ok() {
        return new JsonRes<>(null);
    }

    public JsonRes() {}

    public JsonRes (T data) {
        this.status = 200;
        this.msg = "OK";
        this.data = data;
        this.traceId = MDC.get(LogConstant.TRACE_ID);
    }

    public JsonRes(T data, String msg) {
        this.status = 200;
        this.msg = msg;
        this.data = data;
        this.traceId = MDC.get(LogConstant.TRACE_ID);
    }

    public JsonRes(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
        this.traceId = MDC.get(LogConstant.TRACE_ID);
    }

    // 返回查询结果，一般不需要提示信息
    public static <T> JsonRes<T> ok(T data) {
        return new JsonRes<>(data);
    }

    public static <T> JsonRes<T> ok(T data,String msg) {
        return new JsonRes<>(data,msg);
    }

    // 更新失败，需要数据和提示信息
    public static <T> JsonRes<T> error(T data,String msg) {
        return new JsonRes<>(500, msg, data);
    }

    public static <T> JsonRes<T> error(Integer status,String msg) {
        return new JsonRes<>(status, msg,null);
    }

    // 更新失败，一般需要提示信息
    public static <T> JsonRes<T> errorMsg(String msg) {
        return new JsonRes<>(500, msg, null);
    }

    // 更新成功，并且需要提示信息
    public static <T> JsonRes<T> okMsg(String msg) {
        return new JsonRes<>(200, msg, null);
    }



    public static <T> JsonRes<T> judge(int affectCount , String success, String fail) {
        if (affectCount>0){
            return okMsg(success);
        }else {
            return errorMsg(fail);
        }
    }

    public static <T> JsonRes<T> judge(int affectCount) {
        if (affectCount>0){
            return okMsg("操作成功");
        }else {
            return errorMsg("操作失败");
        }
    }

}
